package com.gph.saviorframework.uitl;

import com.gph.saviorframework.Constants;
import com.gph.saviorframework.common.model.User;
import com.gph.saviorframework.config.service.SubItemService;
import com.gph.saviorframework.security.service.OrgService;
import com.gph.saviorframework.security.service.PositionService;
import com.gph.saviorframework.security.service.UserService;
import com.gph.saviorframework.util.SecUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;

/**
 * excel文件解析器
 */
@Component
public class ExcelParser {

    @Autowired
    private UserService userService;

    @Autowired
    private OrgService orgService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private SecurityUtils securityUtils;

    @Autowired
    private SubItemService subItemService;

//    @Autowired
//    private UserOrclService userOrclService;
    /**
     * 转化用户excel文件
     *1、需要
     * @param excelFile
     * @param orgId
     * @param positionId
     */
    public void parseUserExcelFile(InputStream excelFile, String orgId, String positionId) {
        try {
            Workbook workbook = null;
            List<User> userList = new ArrayList<>();
            workbook = POIUtils.openWorkBook(excelFile);
            //按照规则读取
            if (workbook != null) {
                //获取到第一个worksheet
                Sheet sheet = POIUtils.getSheetByNum(workbook, 0);

                int rowsNum = sheet.getPhysicalNumberOfRows();

                User user = null;
                User existUser = null;
                String email = null;

                for (int i = 1; i < rowsNum; i++) {
                    user = new User();
                    //A列代表的是中文姓名
                    Row row = POIUtils.getRowByIndex(sheet, i);

                    //首先判断用户是否存在，如果邮箱为空的也不导入
                    email = POIUtils.getCellValue(row, "Q");
                    if (StringUtils.isEmpty(email)) {
                        continue;
                    } else {
                        if (userService.existEmail(email)) {
                            continue;
                        }
                    }

                    user.setName(POIUtils.getCellValue(row, "B"));
                    user.setUsername(POIUtils.getCellValue(row, "Q"));
                    user.setEnabled(true);
                    user.setAccountNonExpired(true);
                    user.setAccountNonLocked(true);
                    if (POIUtils.getCellValue(row, "E").equals("男")) {
                        user.setGender("M");
                    } else {
                        user.setGender("F");
                    }
                    user.setCredentialsNonExpired(true);
                    user.setEmail(POIUtils.getCellValue(row, "Q"));
                    user.setBirthday(DateUtils.stringToDate(POIUtils.getCellValue(row, "D"), "yyyy-MM-dd"));
                    user.setIdentity(POIUtils.getCellValue(row, "C"));

                    user.setOrder(0);
                    user.setVersion(0);

                    user.setPassword(SecUtils.getMD5(Constants.DEFAULT_PASSWORD.getBytes()));
                    user.setUserType("USER");//设置为普通用户

                    user.setOrg(orgService.get(orgId));

                    Date date = new Date();
                    user.setCreated(date);
                    user.setCreator(userService.get("admin"));

                    user.setModified(date);
                    user.setModifier(userService.get("admin"));

                    if (StringUtils.isNotEmpty(positionId)) {
                        user.setPosition(positionService.get(positionId));
                    }

                    userList.add(user);
                }
            }
            if (userList.size() > 0) {
                userService.batchSave(userList);
//                userOrclService.batchSave(userList);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     * 导入登船人员
     * 登船人员分两类1
     * 1、已是系统用户直接插入到登船用户表，需要发送短信
     * 2、非系统用户，先插入到系统用户表，再插入到登船用户表
     *3、已存在的登船人员需要重新更新信息
     * @param excelFile
     * @param vesselId
     */
    public String   parseUserStaffExcelFile(HttpServletRequest request,InputStream excelFile, String vesselId) {
        return null;
    }

}
